<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'shop:order:soRds:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.soCode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderSoRdsList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { soRdsDelete, soRdsListData } from '/@/api/shop/order/soRds';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('shop.order.soRds');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('销售订单明细管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('主表ID'),
        field: 'soCode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'cinvname',
        component: 'Input',
      },
      {
        label: t('预发货日期'),
        field: 'dpredate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('数量'),
        field: 'iquantity',
        component: 'Input',
      },
      {
        label: t('辅计量数量'),
        field: 'inum',
        component: 'Input',
      },
      {
        label: t('报价'),
        field: 'iquotedprice',
        component: 'Input',
      },
      {
        label: t('原币无税单价'),
        field: 'iunitprice',
        component: 'Input',
      },
      {
        label: t('原币含税单价'),
        field: 'itaxunitprice',
        component: 'Input',
      },
      {
        label: t('原币无税金额'),
        field: 'imoney',
        component: 'Input',
      },
      {
        label: t('原币税额'),
        field: 'itax',
        component: 'Input',
      },
      {
        label: t('原币价税合计'),
        field: 'isum',
        component: 'Input',
      },
      {
        label: t('原币折扣税'),
        field: 'idiscount',
        component: 'Input',
      },
      {
        label: t('本币无税单价'),
        field: 'inatunitprice',
        component: 'Input',
      },
      {
        label: t('本币无税金额'),
        field: 'inatmoney',
        component: 'Input',
      },
      {
        label: t('本币税额'),
        field: 'inattax',
        component: 'Input',
      },
      {
        label: t('本币价税合计'),
        field: 'inatsum',
        component: 'Input',
      },
      {
        label: t('本币折扣额'),
        field: 'inatdiscount',
        component: 'Input',
      },
      {
        label: t('累计发货辅计量数量'),
        field: 'ifhnum',
        component: 'Input',
      },
      {
        label: t('累计发货数量'),
        field: 'ifhquantity',
        component: 'Input',
      },
      {
        label: t('累计原币发货金额'),
        field: 'ifhmoney',
        component: 'Input',
      },
      {
        label: t('累计开票数量'),
        field: 'ikpquantity',
        component: 'Input',
      },
      {
        label: t('累计开票辅计量数量'),
        field: 'ikpnum',
        component: 'Input',
      },
      {
        label: t('累计原币开票金额'),
        field: 'ikpmoney',
        component: 'Input',
      },
      {
        label: t('备注'),
        field: 'cmemo',
        component: 'Input',
      },
      {
        label: t('扣率'),
        field: 'kl',
        component: 'Input',
      },
      {
        label: t('二次扣率'),
        field: 'kl2',
        component: 'Input',
      },
      {
        label: t('税率'),
        field: 'itaxrate',
        component: 'Input',
      },
      {
        label: t('计量单位编码'),
        field: 'cunitid',
        component: 'Input',
      },
      {
        label: t('采购数量'),
        field: 'fpurquan',
        component: 'Input',
      },
      {
        label: t('零售单价'),
        field: 'fsalecost',
        component: 'Input',
      },
      {
        label: t('零售金额'),
        field: 'fsaleprice',
        component: 'Input',
      },
      {
        label: t('报价单号'),
        field: 'cquocode',
        component: 'Input',
      },
      {
        label: t('预完工日期'),
        field: 'dpremodate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('采购金额'),
        field: 'fpursum',
        component: 'Input',
      },
      {
        label: t('采购发票数量'),
        field: 'fpurbillqty',
        component: 'Input',
      },
      {
        label: t('采购发票金额'),
        field: 'fpurbillsum',
        component: 'Input',
      },
      {
        label: t('已审核订货数量'),
        field: 'fveridispqty',
        component: 'Input',
      },
      {
        label: t('已审核订货原币金额'),
        field: 'fveridispsum',
        component: 'Input',
      },
      {
        label: t('报价含税标识'),
        field: 'bsaleprice',
        component: 'Input',
      },
      {
        label: t('赠品标识'),
        field: 'bgift',
        component: 'Input',
      },
      {
        label: t('使用促销金额'),
        field: 'cbdefine1',
        component: 'Input',
      },
      {
        label: t('业绩金额'),
        field: 'cbdefine2',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'cbdefine3',
        component: 'Input',
      },
      {
        label: t('自定义项22'),
        field: 'cdefine22',
        component: 'Input',
      },
      {
        label: t('自定义项23'),
        field: 'cdefine23',
        component: 'Input',
      },
      {
        label: t('自定义项24'),
        field: 'cdefine24',
        component: 'Input',
      },
      {
        label: t('自定义项25'),
        field: 'cdefine25',
        component: 'Input',
      },
      {
        label: t('自定义项26'),
        field: 'cdefine26',
        component: 'Input',
      },
      {
        label: t('自定义项27'),
        field: 'cdefine27',
        component: 'Input',
      },
      {
        label: t('自定义项28'),
        field: 'cdefine28',
        component: 'Input',
      },
      {
        label: t('自定义项29'),
        field: 'cdefine29',
        component: 'Input',
      },
      {
        label: t('自定义项30'),
        field: 'cdefine30',
        component: 'Input',
      },
      {
        label: t('自定义项31'),
        field: 'cdefine31',
        component: 'Input',
      },
      {
        label: t('自定义项32'),
        field: 'cdefine32',
        component: 'Input',
      },
      {
        label: t('自定义项33'),
        field: 'cdefine33',
        component: 'Input',
      },
      {
        label: t('自定义项34'),
        field: 'cdefine34',
        component: 'Input',
      },
      {
        label: t('自定义项35'),
        field: 'cdefine35',
        component: 'Input',
      },
      {
        label: t('自定义项36'),
        field: 'cdefine36',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('自定义项37'),
        field: 'cdefine37',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('主表ID'),
      dataIndex: 'soCode',
      key: 'a.so_code',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('存货编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'cinvname',
      key: 'a.cinvname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('预发货日期'),
      dataIndex: 'dpredate',
      key: 'a.dpredate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('数量'),
      dataIndex: 'iquantity',
      key: 'a.iquantity',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('辅计量数量'),
      dataIndex: 'inum',
      key: 'a.inum',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('报价'),
      dataIndex: 'iquotedprice',
      key: 'a.iquotedprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('原币无税单价'),
      dataIndex: 'iunitprice',
      key: 'a.iunitprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('原币含税单价'),
      dataIndex: 'itaxunitprice',
      key: 'a.itaxunitprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('原币无税金额'),
      dataIndex: 'imoney',
      key: 'a.imoney',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('原币税额'),
      dataIndex: 'itax',
      key: 'a.itax',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('原币价税合计'),
      dataIndex: 'isum',
      key: 'a.isum',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('原币折扣税'),
      dataIndex: 'idiscount',
      key: 'a.idiscount',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('本币无税单价'),
      dataIndex: 'inatunitprice',
      key: 'a.inatunitprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('本币无税金额'),
      dataIndex: 'inatmoney',
      key: 'a.inatmoney',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('本币税额'),
      dataIndex: 'inattax',
      key: 'a.inattax',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('本币价税合计'),
      dataIndex: 'inatsum',
      key: 'a.inatsum',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('本币折扣额'),
      dataIndex: 'inatdiscount',
      key: 'a.inatdiscount',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('累计发货辅计量数量'),
      dataIndex: 'ifhnum',
      key: 'a.ifhnum',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('累计发货数量'),
      dataIndex: 'ifhquantity',
      key: 'a.ifhquantity',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('累计原币发货金额'),
      dataIndex: 'ifhmoney',
      key: 'a.ifhmoney',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('累计开票数量'),
      dataIndex: 'ikpquantity',
      key: 'a.ikpquantity',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('累计开票辅计量数量'),
      dataIndex: 'ikpnum',
      key: 'a.ikpnum',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('累计原币开票金额'),
      dataIndex: 'ikpmoney',
      key: 'a.ikpmoney',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('备注'),
      dataIndex: 'cmemo',
      key: 'a.cmemo',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('扣率'),
      dataIndex: 'kl',
      key: 'a.kl',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('二次扣率'),
      dataIndex: 'kl2',
      key: 'a.kl2',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('税率'),
      dataIndex: 'itaxrate',
      key: 'a.itaxrate',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('计量单位编码'),
      dataIndex: 'cunitid',
      key: 'a.cunitid',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('采购数量'),
      dataIndex: 'fpurquan',
      key: 'a.fpurquan',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('零售单价'),
      dataIndex: 'fsalecost',
      key: 'a.fsalecost',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('零售金额'),
      dataIndex: 'fsaleprice',
      key: 'a.fsaleprice',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('报价单号'),
      dataIndex: 'cquocode',
      key: 'a.cquocode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('预完工日期'),
      dataIndex: 'dpremodate',
      key: 'a.dpremodate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('采购金额'),
      dataIndex: 'fpursum',
      key: 'a.fpursum',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('采购发票数量'),
      dataIndex: 'fpurbillqty',
      key: 'a.fpurbillqty',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('采购发票金额'),
      dataIndex: 'fpurbillsum',
      key: 'a.fpurbillsum',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('已审核订货数量'),
      dataIndex: 'fveridispqty',
      key: 'a.fveridispqty',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('已审核订货原币金额'),
      dataIndex: 'fveridispsum',
      key: 'a.fveridispsum',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('报价含税标识'),
      dataIndex: 'bsaleprice',
      key: 'a.bsaleprice',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('赠品标识'),
      dataIndex: 'bgift',
      key: 'a.bgift',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('使用促销金额'),
      dataIndex: 'cbdefine1',
      key: 'a.cbdefine1',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('业绩金额'),
      dataIndex: 'cbdefine2',
      key: 'a.cbdefine2',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('仓库'),
      dataIndex: 'cbdefine3',
      key: 'a.cbdefine3',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项22'),
      dataIndex: 'cdefine22',
      key: 'a.cdefine22',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项23'),
      dataIndex: 'cdefine23',
      key: 'a.cdefine23',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项24'),
      dataIndex: 'cdefine24',
      key: 'a.cdefine24',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项25'),
      dataIndex: 'cdefine25',
      key: 'a.cdefine25',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项26'),
      dataIndex: 'cdefine26',
      key: 'a.cdefine26',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('自定义项27'),
      dataIndex: 'cdefine27',
      key: 'a.cdefine27',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('自定义项28'),
      dataIndex: 'cdefine28',
      key: 'a.cdefine28',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项29'),
      dataIndex: 'cdefine29',
      key: 'a.cdefine29',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项30'),
      dataIndex: 'cdefine30',
      key: 'a.cdefine30',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项31'),
      dataIndex: 'cdefine31',
      key: 'a.cdefine31',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项32'),
      dataIndex: 'cdefine32',
      key: 'a.cdefine32',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项33'),
      dataIndex: 'cdefine33',
      key: 'a.cdefine33',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自定义项34'),
      dataIndex: 'cdefine34',
      key: 'a.cdefine34',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项35'),
      dataIndex: 'cdefine35',
      key: 'a.cdefine35',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项36'),
      dataIndex: 'cdefine36',
      key: 'a.cdefine36',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('自定义项37'),
      dataIndex: 'cdefine37',
      key: 'a.cdefine37',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑销售订单明细'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:order:soRds:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除销售订单明细'),
        popConfirm: {
          title: t('是否确认删除销售订单明细'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:order:soRds:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: soRdsListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await soRdsDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
